/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Base for creating a custom VGUI class; remove the functions you don't use.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	//REWORK this to work with a SENT

local PANEL = {}

function PANEL:Init()
	//Create controls here

	self:SetTitle( " " )
	self:ShowCloseButton( false )
	
	self.ENT = nil
	self.ANCRanks = {}
	
	local ANCForm = vgui.Create( "DForm", self )
		ANCForm:SetSpacing( 5 )
		ANCForm:SetPadding( 5 )
		ANCForm:SetName( "Anti-NoClip Options" )
		
		self.ANCForm = ANCForm
		
	
	local ANCToggle = vgui.Create( "DCheckBoxLabel" )
		ANCToggle:SetText( "Toggle Anti-NoClip" )
		ANCForm:AddItem( ANCToggle )
		
		self.ANCToggle = ANCToggle
		
		
	local UseRanks = vgui.Create( "DCheckBoxLabel" )
		UseRanks:SetText( "Derive settings from ranks" )
		ANCForm:AddItem( UseRanks )
		
		self.UseRanks = UseRanks
		
		
	local UseFriends = vgui.Create( "DCheckBoxLabel" )
		UseFriends:SetText( "Derive settings from PP friends" )
		ANCForm:AddItem( UseFriends )
		
		self.UseFriends = UseFriends
		
		
	local RankList = vgui.Create( "DPanelList" )
		RankList:SetSpacing( 5 )
		RankList:SetPadding( 2 )
		RankList:EnableHorizontal( false )
		RankList:EnableVerticalScrollbar( true )
		
		self.RankList = RankList
		
		
	local SaveB = vgui.Create( "DButton", self )
	local SP = self
		SaveB:SetText( "Save and Exit" )
		function SaveB:DoClick()
			SP:GetANCHandle( SP.ENT ).Options = { "Toggled" = SP.ANCToggle:GetChecked(), "UseRanks" = SP.UseRanks:GetChecked(), "UseFriends" = SP.UseFriends:GetChecked(), "RANKS" = SP.ANCRanks }
			SP:Remove()
		end
		
		self.SaveB = SaveB
		
		
	local ExitB = vgui.Create( "DButton", self )
		ExitB:SetText( "Exit Without Saving" )
		function() ExitB:DoClick()
			SP:Remove()
		end
		
		self.ExitB = ExitB
		
	self.MakePopup()
	self.Center()
		
end

function PANEL:AddRanks()

	for k,v in pairs( RankTable ) do
	
		local RankBox = vgui.Create( "DCheckBoxLabel" )
			RankBox:SetText( v )
			RankBox:SetChecked( table.HasValue( self.ANCRanks, v ) )
			function RankBox:OnChange()
				if( RankBox:GetChecked() ) then
					if !table.HasValue( self.ANCRanks, v ) then table.insert( v ) end
				else
					if table.HasValue( self.ANCRanks, v ) then
						for rn,r in pairs( self.ANCRanks ) do
							if r == v then table.remove( self.ANCRanks, rn ) break end
						end
					end
				end
			end
		
		self.RankList:AddItem( RankBox )
	end

end

function PANEL:GetANCHandle( ent )
	if ent and ent:IsValid() and ent.ANCEnt & ent.ANCEnt:IsValid() then return ent.ANCEnt end
	
	local ANC = ents.Create( "ANCEntity" )
		ANC:ReplaceENT( ent )
		ANC:Spawn()
		
		ent.ANCEnt = ANC
		
		return ANC
end

function PANEL:PerformLayout()
	
	self.ANCForm:StretchToParent( 0, 0, 0, self:GetTall() * 0.25 )
	
	self.SaveB:AlignBottom( 5 )
	self.SaveB:AlignLeft( 5 )
	
	self.ExitB:AlignBottom( 5 )
	self.ExitB:AlignLeft( 5 )

end

function PANEL:Paint()
	//Do Painting here
	
	self.BaseClass.Paint( self )

end

function PANEL:PaintOver()

	//Paint over self and child elements here.

end

function PANEL:Think()

	//Think stuff here

end

function PANEL:OnCursorEntered()

	//Do stuff when the cursor enters the panel

end

function PANEL:OnCursorExited()

	//Do Stuff when the cursor leaves the panel

end

function PANEL:OnMouseWheeled()

	//Do stuff when you wheel the mouse on this panel

end

function PANEL:OnMousePressed()

	//Do stuff when you press the mouse on this panel

end

function PANEL:OnMouseReleased()

	//Do stuff when you release the mouse on this panel

end

function PANEL:OnKeyCodePressed()

	//Do stuff when using keys from the keyboard on this panel. This hook uses the keycode enumerations
	//http://wiki.garrysmod.com/?title=P.OnKeyCodePressed	For more information

end

function PANEL:ActionSignal()

	//This is called on the parent frame when a child VGUI element is used. The key corresponds to what is set by Panel.SetCommand.
	//From the wiki
	
end

function PANEL:OnCursorMoved()

	//From the wiki
	//Called whenever the the mouse is moved within the panel.

end

function PANEL:DoClick()

	//For button type panels, use this. Called when you click the button.

end


//Register syntax - Desired element name, Panel to use, base class of the new element.
vgui.Register( "AntiNC", PANEL, "DFrame" )

function PlayerANCHOOK( um )
	local ENT = um:ReadEntity()
	local ANCRANKS = um:ReadString()
	
	local Menu = vgui.Create( "DMenu" )
		Menu:AddOption( "Anti-NoClip Menu", function()
			local ANCMenu = vgui.Create( "AntiNC" )
				ANCMenu.ENT = ENT
				if ENT.ANCEnt and ENT.ANCEnt:IsValid() then
					ANCMenu.ANCRanks( ENT.Options.RANKS )
				else
					ANCMenu.ANCRanks = { ANCRANKS }
				end
				
				if ENT.ANCEnt and ENT.ANCEnt:IsValid() then
					ANCMenu.UseRanks:SetChecked( ENT.Options.UseRanks )
				end
				
				if ENT.ANCEnt and ENT.ANCEnt:IsValid() then
					ANCMenu.UseFriends:SetChecked( ENT.Options.UseFriends )
				end
				ANCMenu:AddRanks()
				ANCMenu:MakePopup()
			end )
		Menu:AddOption( "Exit", function() Menu:Remove() end )
		Menu:SizeToContents()
		Menu:Center()
		Menu:MakePopup()
end
usermessage.Hook( "PlayerANCMESS", PlayerANCHOOK )